var API_URL='https://m3.xiaoyu.com/welcome/wechatapp';
var WxParse = require('../../wxParse/wxParse.js');

Page({
  data: {
    errCode:0,
    threadinfo:[],
    threadreply:[],
    tid:null,
    paged:1,
    loading:false,
    loadend:false,
    loaded:false,
    sharetitle:''
  },
  onLoad: function (params) {
    this.setData({
      tid:params.tid
    });
    var _this =this;
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000
    });

    wx.request({
      url:API_URL,
      data:{
        callback:"thread.info",
        tid:params.tid,
        page:1
      },
      header: {
          'content-type': 'application/json'
      },
      success: function(res) {
        wx.hideToast();
        var data = res.data;
        WxParse.wxParse('threadinfocontent','html',data.data.thread.content,_this);
        
        if(data.data.count>0){
          for (var i = 0; i < data.data.list.length; i++) {
            WxParse.wxParse('reply' + i, 'html', data.data.list[i].content, _this);
  		  
            if (i == data.data.list.length - 1) {
              WxParse.wxParseTemArray("replyTemArray",'reply', data.data.list.length, _this)
            }
          } 
        };
		
        _this.setData({
          errCode:data.errCode,
          threadinfo:data.data.thread,
          threadreply:data.data.list,
          loaded: true,
          sharetitle:data.data.thread.subject
        });
        console.log(data);
      }
    })
  },
  onReachBottom: function(){

    var paged = this.data.paged + 1;
    var _this =this;
    _this.setData({
      loading: true
    });

    wx.request({
      url:API_URL,
      data:{
        callback:"thread.info",
        tid:_this.data.tid,
        page:paged
      },
      header: {
          'content-type': 'application/json'
      },
      success: function(res) {
        var data = res.data;
        console.log('叔叔叔叔');
        console.log(data.data);
        console.log(data.data.count);
        if(data.data.count>((paged-1)*20)){

          _this.setData({
            paged: paged,
            threadreply:_this.data.threadreply.concat(data.data.list),
            loading: false
          });
          for (var i = 0; i < _this.data.threadreply.length; i++) {
            WxParse.wxParse('reply' + i, 'html', _this.data.threadreply[i].content, _this);
            if (i === _this.data.threadreply.length - 1) {
              WxParse.wxParseTemArray("replyTemArray",'reply', _this.data.threadreply.length, _this)
            }
          };

        }
        else{
          _this.setData({
            loading: false,
            loadend: true
          });
        };
      }
    })
	
  },

  onPullDownRefresh: function () {
    var _this =this;
    wx.request({
      url:API_URL,
      data:{
        callback:"thread.info",
        tid:_this.data.tid,
        page:1
      },
      header: {
          'content-type': 'application/json'
      },
      success: function(res) {
        var data = res.data;
        WxParse.wxParse('threadinfocontent','html',data.data.thread.content,_this);
        
        if(data.data.count>0){
          for (var i = 0; i < data.data.list.length; i++) {
            WxParse.wxParse('reply' + i, 'html', data.data.list[i].content, _this);
        
            if (i == data.data.list.length - 1) {
              WxParse.wxParseTemArray("replyTemArray",'reply', data.data.list.length, _this)
            }
          } 
        };
    
        _this.setData({
          errCode:data.errCode,
          threadinfo:data.data.thread,
          threadreply:data.data.list
        });
        wx.stopPullDownRefresh(); //停止下拉刷新
      }
    })
  },

  onShareAppMessage: function () {
    return {
      title: this.data.sharetitle,
      desc: '',
      path: 'pages/detail/detail?tid='+this.data.tid,
    }
  }
})